######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################

# -----------------------------------------------------------------
# Creates an object used to manipulate the Performance Point Service
# -----------------------------------------------------------------
function New-PerformancePointService()
{
    $obj=new-object PSObject -Property @{
	  DB = New-Database;
      Name = "PerformancePoint Service";
      Proxy = "PerformancePoint Service Proxy";
      Pool = New-ApplicationPool;
      Service = New-SupportingService;
      AnalyticQueryCellMax = 100000;
      AnalyticQueryLoggingEnabled = $false;
      ApplicationCacheEnabled = $true;
      ApplicationCacheMinimumHitCount = 2;
      ApplicationProxyCacheEnabled = $true;
      CommentsDisabled = $false;
      CommentsScorecardMax = 1000;
      DataSourceQueryTimeoutSeconds = 300;
      DecompositionTreeMaximum = 25;
      ElementCacheSeconds = 15;
      FilterRememberUserSelectionsDays = 90;
      FilterTreeMembersMax = 500;
      IndicatorImageCacheSeconds = 10;
      SelectMeasureMaximum = 1000;
      SessionHistoryHours = 2;
      ShowDetailsInitialRows = 10000;
      ShowDetailsMaxRows = 10000;
      ShowDetailsMaxRowsDisabled = $false;
      TrustedContentLocationsRestricted = $false;
      TrustedDataSourceLocationsRestricted = $false;
      Default =$true;
    }
    $obj.Service.Name = "PerformancePoint Service"
    # -----------------------------------------------------------------
    # Validates the information in the object
    # -----------------------------------------------------------------
    Add-Method $obj "Validate" `
    {
        Write-Operation "Validating Performance Point Service"
		$this.DB.Validate()
        $this.Pool.Validate()
        $this.Service.Validate()
        Validate-NotEmpty "Name" $this.Name
        Validate-NotEmpty "Proxy" $this.Proxy 
        Validate-Range "AnalyticQueryCellMax" $this.AnalyticQueryCellMax 1 1000000000
        Validate-Range "CommentsScorecardMax" $this.CommentsScorecardMax 1 1000000
        Validate-Range "DataSourceQueryTimeoutSeconds" $this.DataSourceQueryTimeoutSeconds 1 36000
        Validate-Range "DecompositionTreeMaximum" $this.DecompositionTreeMaximum 1 1000000
        Validate-Range "FilterRememberUserSelectionsDays" $this.FilterRememberUserSelectionsDays 1 10000
        Validate-Range "FilterTreeMembersMax" $this.FilterTreeMembersMax 1 100000
        Validate-Range "IndicatorImageCacheSeconds" $this.IndicatorImageCacheSeconds 1 36000
        Validate-Range "SelectMeasureMaximum" $this.SelectMeasureMaximum 1 1000000
        Validate-Range "SessionHistoryHours" $this.SessionHistoryHours 1 48
        Validate-Range "ShowDetailsInitialRows" $this.ShowDetailsInitialRows 1 100000
        Validate-Range "ShowDetailsMaxRows" $this.ShowDetailsMaxRows 1 1000000
    }
    # -----------------------------------------------------------------
    # Provisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Provision" `
    {
        $sa = Get-SPServiceApplication  | Where-Object {$_.DisplayName -eq $this.Name}

        if($sa -eq $null)
        {
            Write-Operation "Creating $($this.Name)"
            $pool = $this.Pool.Provision()
			$this.DB.Provision()
            
            $sa = New-SPPerformancePointServiceApplication -Name $this.Name `
                    -ApplicationPool $pool `
                    -AnalyticQueryCellMax $this.AnalyticQueryCellMax `
                    -AnalyticQueryLoggingEnabled:$this.AnalyticQueryLoggingEnabled `
                    -ApplicationCacheEnabled:$this.ApplicationCacheEnabled `
                    -ApplicationCacheMinimumHitCount $this.ApplicationCacheMinimumHitCount `
                    -ApplicationProxyCacheEnabled:$this.ApplicationProxyCacheEnabled `
                    -CommentsDisabled:$this.CommentsDisabled `
                    -CommentsScorecardMax $this.CommentsScorecardMax `
                    -DataSourceQueryTimeoutSeconds $this.DataSourceQueryTimeoutSeconds `
                    -DecompositionTreeMaximum $this.DecompositionTreeMaximum `
                    -ElementCacheSeconds $this.ElementCacheSeconds `
                    -FilterRememberUserSelectionsDays $this.FilterRememberUserSelectionsDays `
                    -FilterTreeMembersMax $this.FilterTreeMembersMax `
                    -IndicatorImageCacheSeconds $this.IndicatorImageCacheSeconds `
                    -SelectMeasureMaximum $this.SelectMeasureMaximum `
                    -SessionHistoryHours $this.SessionHistoryHours `
                    -ShowDetailsInitialRows $this.ShowDetailsInitialRows `
                    -ShowDetailsMaxRows $this.ShowDetailsMaxRows `
                    -ShowDetailsMaxRowsDisabled:$this.ShowDetailsMaxRowsDisabled `
                    -TrustedContentLocationsRestricted:$this.TrustedContentLocationsRestricted `
                    -TrustedDataSourceLocationsRestricted:$this.TrustedDataSourceLocationsRestricted `
					-DatabaseServer $this.DB.Server `
					-DatabaseName $this.DB.FullName `
					-DatabaseFailoverServer $this.DB.FailoverServer
        }
        else
        {
            Write-OperationDone "$($this.Name) has already been provisioned"  
        } 
         
        $proxy = Get-SPServiceApplicationProxy  | Where-Object {$_.DisplayName -eq $this.Proxy}  
        if($proxy -eq $null)
        {
            New-SPPerformancePointServiceApplicationProxy -Name $this.Proxy `
                -ServiceApplication $sa `
                -Default:$this.Default 
        }
        else
        {
            Write-OperationDone "$($this.Proxy) has already been provisioned"  
        }
        $this.Service.Provision()
        return $sa
    }
    # -----------------------------------------------------------------
    # Unprovisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Unprovision" `
    {
        Remove-ServiceApplicationProxy $this.Proxy
        Remove-ServiceApplication $this.Name
        $this.Service.Unprovision()
    }
    return $obj
}
